package com.hw.one.peach.core.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hw.one.dam2.common.SiteTypeEnum;
import com.hw.one.dam2.dao.IRegionSiteDao;
import com.hw.one.dam2.param.RegionSiteParam;
import com.hw.one.dam2.param.SiteAirParam;
import com.hw.one.dam2.util.WeatherUtil;
import com.hw.one.ext.util.UserUtil;
import com.hw.one.peach.core.dao.IRaiseDustDao;
import com.hw.one.peach.core.po.RaiseDustPO;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by Administrator on 2017/8/14.
 */
@Service(value = "raiseDustDataService")
public class RaiseDustDataService implements IRaiseDustDataService{

    @Resource
    private IRegionSiteDao regionSiteDao;

    @Resource
    private IRaiseDustDao raiseDustDao;
    @Override
    public Object findRaiseDataRealData(Map<String, Object> paramMap) throws Exception {
        Map<String, Object> region = UserUtil.getRegion();
        String userRegionCode = (String) region.get("regionCode");   //当前用户所在的区域
        String userRegionName = (String) region.get("regionName");

        String pointType = (String) paramMap.get("siteType");
        /**
         * 根据当前登录用户区域，查询该区域的上级区域
         */
        List<Map<String, Object>> parentRegionList = regionSiteDao.findParentRegionInfoByRegionCode(userRegionCode);

        // 地图的中心点位
        JSONObject centerPointObj = new JSONObject();
        for (Map<String, Object> parentRegionMap : parentRegionList) {
            String regionCode = String.valueOf(parentRegionMap.get("regionCode"));
            if (userRegionCode.equals(regionCode)) {
                centerPointObj.put("lng", parentRegionMap.get("longitude"));
                centerPointObj.put("lat", parentRegionMap.get("latitude"));
            }
        }


        RegionSiteParam siteParam = new RegionSiteParam();
        siteParam.setRegionCode(userRegionCode);  //当前用户的所在城市的区域编码
        siteParam.setSiteType(pointType);   //
        List<Map<String, Object>> siteInfoList = regionSiteDao.findSiteInfoByRegionCodeAndSiteType(siteParam);

        if (null == siteInfoList || siteInfoList.size() == 0) {
            return new HashMap<>();
        }

        List<String> siteCodeList = new ArrayList<String>();
        List<String> snCodeList = new ArrayList<String>();
        for (Map<String, Object> siteMap : siteInfoList) {
            siteCodeList.add(String.valueOf(siteMap.get("siteCode")));
            snCodeList.add(String.valueOf(siteMap.get("SNCode")));
        }

        List<Map<String, Object>> raiseDustMapList = new ArrayList<>();
        List<Map<String, Object>> raiseDustNoDataMapList = new ArrayList<>();
        if (SiteTypeEnum.RAISE_DUST_NEW_SITE.getValue().equals(pointType)) {
            // 市建委扬尘数据
            SiteAirParam siteAirParam = new SiteAirParam();
            siteAirParam.setSiteCodes(siteCodeList);
            List<RaiseDustPO> raiseDustPOList = raiseDustDao.findRaiseDustRealDataBySiteCodes(siteAirParam);

            List<String> siteHaveDataList = new ArrayList<>();
            for (Map<String, Object> siteInfo : siteInfoList) {
                boolean flag = false;
                String siteCode = (String) siteInfo.get("siteCode");
                if (siteHaveDataList.contains(siteCode)) {
                    continue;
                }

                for (RaiseDustPO raiseDustPO : raiseDustPOList) {
                    JSONObject hourPO = new JSONObject();
                    if (siteCode.equals(raiseDustPO.getSITECODE())) {

                        // 从数据库中读取的时间
                        String oragMonitorTime = raiseDustPO.getMONITORTIME2();
                        if (null != oragMonitorTime) {
                            hourPO.put("monitorTime", oragMonitorTime.substring(0, 16));
                        }
                        hourPO.put("siteName", siteInfo.get("siteName"));
                        hourPO.put("siteCode", siteInfo.get("siteCode"));
                        hourPO.put("siteTypeCode", siteInfo.get("siteTypeCode"));
                        hourPO.put("Latitude", siteInfo.get("Latitude"));
                        hourPO.put("Longitude", siteInfo.get("Longitude"));
                        hourPO.put("PM10", raiseDustPO.getPM10());
                        hourPO.put("PM2_5", raiseDustPO.getPM25());
                        hourPO.put("temp", raiseDustPO.getTEMP());
                        hourPO.put("hum", raiseDustPO.getHUM());
                        // 风速格式化成多少级
                        hourPO.put("windSpeed", WeatherUtil.changeWindSpeedToPowerNoCN(raiseDustPO.getWS()));
                        hourPO.put("windDirection", raiseDustPO.getWD());

                        if (!isOrNotExceptionData(raiseDustPO)) {
                            siteHaveDataList.add(siteCode);
                            flag = true;
                            putSiteStatus(hourPO, "1111"); // 数据正常
                            // 结果集
                            raiseDustMapList.add(hourPO);
                            break;
                        }
                    }
                }
                // 如果没有数据
                if (!flag) {
                    Map<String, Object> hourPO = new HashMap<>();
                    hourPO.put("siteName", siteInfo.get("siteName"));
                    hourPO.put("siteCode", siteInfo.get("siteCode"));
                    hourPO.put("siteTypeCode", siteInfo.get("siteTypeCode"));
                    hourPO.put("Latitude", siteInfo.get("Latitude"));
                    hourPO.put("Longitude", siteInfo.get("Longitude"));
                    hourPO.put("PM10", "-");
                    hourPO.put("PM2_5", "-");
                    hourPO.put("temp", "-");
                    hourPO.put("hum", "-");
                    hourPO.put("monitorTime", "-");
                    // 风速格式化成多少级
                    hourPO.put("windSpeed", "-");
                    hourPO.put("windDirection", "-");
                    putSiteStatus(hourPO, "0000"); // 数据异常
                    raiseDustNoDataMapList.add(hourPO);
                }
            }
            String sortStr = "PM10";
            Collections.sort(raiseDustMapList, (a, b) -> {
                if (null == a.get(sortStr) || "-".equals(String.valueOf(a.get(sortStr)))) {
                    return -1;
                }
                if (null == b.get(sortStr) || "-".equals(String.valueOf(b.get(sortStr)))) {
                    return -1;
                }
                Integer one = Integer.valueOf(a.get(sortStr).toString());
                Integer two = Integer.valueOf(b.get(sortStr).toString());
                return two.compareTo(one);
            });
            raiseDustMapList.addAll(raiseDustNoDataMapList);
        }else {
            return new HashMap<>();
        }
        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put("siteList", raiseDustMapList);        //返回的数据
        returnMap.put("centerSite", centerPointObj);  //中心城市点位
        returnMap.put("centerName", userRegionName);  //用户地图名称
        return returnMap;
    }


    /**
     * status :
     * 1111 正常数据
     * 0000 异常数据(数据库有,但是未0的数据)
     * 0001 未记录数据(数据库没有的数据)
     *
     * @param map
     * @param status
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public void putSiteStatus(Map map, String status) {
        map.put("siteStatus", status);
    }
    public boolean isOrNotExceptionData(RaiseDustPO raiseDust) {
        return isEmpty(raiseDust.getPM10()) && isEmpty(raiseDust.getPM25()) &&
                isEmpty(raiseDust.getTEMP()) && isEmpty(raiseDust.getHUM()) &&
                isEmpty(raiseDust.getNOISE()) && isEmpty(raiseDust.getWS()) &&
                isEmpty(raiseDust.getWD());
    }

    public boolean isEmpty(String str) {
        return null == str || "-".equals(str) || "0".equals(str) || "0.0".equals(str) || "0.00".equals(str);
    }
}
